<?php  
class ControllerCheckoutPayment extends Controller {
	var $error = array();
	
  	function index() {
		$address   =& $this->locator->get('address');
		$cart      =& $this->locator->get('cart');
		$config    =& $this->locator->get('config');
		$customer  =& $this->locator->get('customer');
		$database  =& $this->locator->get('database');
		$language  =& $this->locator->get('language');
		$module    =& $this->locator->get('module');
		$payment   =& $this->locator->get('payment');
		$request   =& $this->locator->get('request');
		$response  =& $this->locator->get('response');
		$session   =& $this->locator->get('session');
		$shipping  =& $this->locator->get('shipping');
		$tax       =& $this->locator->get('tax');
		$template  =& $this->locator->get('template');
		$url       =& $this->locator->get('url');
		 
    	if (!$customer->isLogged()) {
			$session->set('redirect', $url->ssl('checkout_shipping'));
			
	  		$response->redirect($url->ssl('account_login'));
    	} 
 
    	if ((!$cart->hasProducts()) || ((!$cart->hasStock()) && (!$config->get('config_stock_checkout') && ($config->get('config_stock_check'))))) {
      		$response->redirect($url->ssl('cart'));
    	}

    	if ($cart->hasShipping()) {
			if (!$session->get('shipping_method')) {
	  			$response->redirect($url->ssl('checkout_shipping'));
			}
			
			if (!$shipping->getQuote($session->get('shipping_method'))) {
	  			$response->redirect($url->ssl('checkout_shipping'));
    		}
			
			if (!$address->has($session->get('shipping_address_id'))) {
	  			$response->redirect($url->ssl('checkout_address', 'shipping'));
    		}
		} else {
			$session->delete('shipping_address_id');
			$session->delete('shipping_method');
		}
			
    	if (!$address->has($session->get('payment_address_id'))) {
      		$session->set('payment_address_id', $session->get('shipping_address_id'));
    	}
	
    	if (!$address->has($session->get('payment_address_id'))) {
	  		$session->set('payment_address_id', $customer->getAddressId());
    	}

    	if (!$session->get('payment_address_id')) {
	  		$response->redirect($url->ssl('checkout_address', 'payment'));
    	}

    	$language->load('controller/checkout_payment.php');

    	$template->set('title', $language->get('heading_title')); 
		  
    	if ($request->isPost() && $request->has('payment', 'post') && $this->validate()) {
			$session->set('payment_method', $request->gethtml('payment', 'post'));
		  
	  	  	$session->set('comment', strip_tags($request->gethtml('comment', 'post')));
		  
	  		$response->redirect($url->ssl('checkout_confirm'));
    	}
	
    	$view = $this->locator->create('template');

		$view->set('tpl', $template);

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('text_payment', $language->get('text_payment'));
    	$view->set('text_payment_to', $language->get('text_payment_to'));
    	$view->set('text_payment_address', $language->get('text_payment_address'));
    	$view->set('text_payment_method', $language->get('text_payment_method'));
    	$view->set('text_payment_methods', $language->get('text_payment_methods'));
    	$view->set('text_comments', $language->get('text_comments'));

    	$view->set('button_change_address', $language->get('button_change_address'));
    	$view->set('button_continue', $language->get('button_continue'));
    	$view->set('button_back', $language->get('button_back'));

    	$view->set('error', @$this->error['message']);

    	$view->set('action', $url->ssl('checkout_payment'));
    
    	$view->set('address', $address->getFormatted($session->get('payment_address_id'), '<br />'));
    
		$view->set('change_address', $url->ssl('checkout_address', 'payment'));
    
        $session->delete('payment_form_enctype');
    
		$view->set('methods', $payment->getMethods());

    	$view->set('default', $session->get('payment_method'));
 
    	$view->set('comment', $session->get('comment'));

    	if ($cart->hasShipping()) {
      		$view->set('back', $url->ssl('checkout_shipping'));
    	} else {
      		$view->set('back', $url->ssl('cart'));
    	}
	
		$template->set('content', $view->fetch('content/checkout_payment.tpl'));
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));
  	}
  
  	function validate() {
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
			    
    	if (!$request->has('payment', 'post')) {
	  		$this->error['message'] = $language->get('error_payment');
		}
		
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}  
}
?>
